package com.example.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@RestController(value = "/api")
public class Api {
    @PostMapping(path = "/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        response.addCookie(new Cookie("user", "wangjie"));
        return "success";
    }
    @GetMapping(path = "/list")
    public List<String> list(HttpServletRequest request, HttpServletResponse response) {
        if(request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                System.out.println("cookies: " + cookie.getName() + ":"+cookie.getValue());
            }
            return Arrays.asList("AAA", "BBB");
        } else {
            return Arrays.asList();
        }
    }
}
